/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.course;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.config.Global;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourseContent;
import com.idenger.modules.edu.service.course.EduCourseContentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.persistence.Page;
import com.idenger.common.utils.StringUtils;

/**
 * 视频Controller
 * @author chenlin
 * @version 2017-12-01
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/course/eduCourseContent")
public class EduCourseContentController extends BaseController {

	@Autowired
	private EduCourseContentService eduCourseContentService;
	
	@ModelAttribute
	public EduCourseContent get(@RequestParam(required=false) String id) {
		EduCourseContent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduCourseContentService.get(id);
		}
		if (entity == null){
			entity = new EduCourseContent();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:course:eduCourseContent:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduCourseContent eduCourseContent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduCourseContent> page = eduCourseContentService.findPage(new Page<EduCourseContent>(request, response), eduCourseContent); 
		model.addAttribute("page", page);
		return "modules/edu/course/eduCourseContentList";
	}

	@RequiresPermissions("edu:course:eduCourseContent:view")
	@RequestMapping(value = "form")
	public String form(EduCourseContent eduCourseContent, Model model, HttpServletRequest request, HttpServletResponse response) {
			
		String courseId =	request.getParameter("courseId");
		model.addAttribute("eduCourseContent", eduCourseContent);
		model.addAttribute("courseId",courseId);
		return "modules/edu/course/eduCourseContentForm";
	}

	@RequiresPermissions("edu:course:eduCourseContent:edit")
	@RequestMapping(value = "save")
	public String save(EduCourseContent eduCourseContent, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response) {
		if (!beanValidator(model, eduCourseContent)){
			return form(eduCourseContent, model,request,response);
		}
		String courseId= request.getParameter("courseId");
		
		eduCourseContentService.save(eduCourseContent);
		addMessage(redirectAttributes, "保存视频成功");
		System.out.println(courseId+"courseId");
		return "redirect:"+ Global.getAdminPath()+"/edu/course/eduSection/?repage&coursrId="+courseId;
	}
	
	@RequiresPermissions("edu:course:eduCourseContent:edit")
	@RequestMapping(value = "save2")
	public String save2(EduCourseContent eduCourseContent, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response) {
		BaseResponseMsg baseResponseMsg;
		eduCourseContentService.save(eduCourseContent);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduCourseContent);
		return renderString(response, baseResponseMsg);
	}

	@RequiresPermissions("edu:course:eduCourseContent:edit")
	@RequestMapping(value = "delete2")
	public String delete2(EduCourseContent eduCourseContent, HttpServletResponse response) {
		BaseResponseMsg baseResponseMsg;
		eduCourseContentService.delete(eduCourseContent);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		return renderString(response, baseResponseMsg);
	}


	@RequiresPermissions("edu:course:eduCourseContent:edit")
	@RequestMapping(value = "delete")
	public String delete(EduCourseContent eduCourseContent, RedirectAttributes redirectAttributes) {
		eduCourseContentService.delete(eduCourseContent);
		addMessage(redirectAttributes, "删除视频成功");
		return "redirect:"+Global.getAdminPath()+"/edu/course/eduCourseContent/?repage";
	}

}